package com.fangcun.controller.admin.old;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fangcun.bean.admin.order.request.OrderDeliveredRequest;
import com.fangcun.bean.admin.order.request.OrderRequest;
import com.fangcun.bean.sys.PageBean;
import com.fangcun.bean.sys.ResponseMsg;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProAffairException;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.util.PublicMethod;
import com.fangcun.common.util.apiversion.ApiVersion;
import com.fangcun.common.util.file.FileUtils;
import com.fangcun.entity.OrderCardNumber;
import com.fangcun.service.admin.OrderSerivce;
import com.fangcun.service.admin.OrderSubscribeService;

/** 后台-订单 **/
@RestController
@RequestMapping("/admin/{version}/auth/order")
public class OrderController {

	private static Logger LOG = LoggerFactory.getLogger(OrderController.class);

	@Autowired
	private OrderSerivce orderSerivce;
	@Autowired
	private OrderSubscribeService orderSubscribeService;

	@ApiVersion(1.0)
	@RequestMapping(value = "/list")
	private ResponseMsg list(@RequestBody PageBean<OrderRequest> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(orderSerivce.list(request));
		} catch (ProException e) {
			LOG.error("-订单列表出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-订单列表异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping(value = "/details")
	private ResponseMsg details(Long id) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(orderSerivce.details(id));
		} catch (ProException e) {
			LOG.error("-订单详情出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-订单详情异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping(value = "/orderDelivered")
	private ResponseMsg orderDelivered(@RequestBody OrderDeliveredRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			orderSerivce.orderDelivered(request);
		} catch (ProException e) {
			LOG.error("-订单改为已发货出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-订单改为已发货异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/****/
	@ApiVersion(1.0)
	@RequestMapping(value = "cardnumber/list")
	private ResponseMsg cardnumberList(@RequestBody PageBean<OrderCardNumber> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(orderSubscribeService.list(request));
		} catch (ProException e) {
			LOG.error("-卡号列表->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-卡号列表->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/****/
	@ApiVersion(1.0)
	@RequestMapping(value = "cardnumber/details")
	private ResponseMsg subscribeDetails(Long orderDetailId) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(orderSubscribeService.details(orderDetailId));
		} catch (ProException e) {
			LOG.error("-针对已使用的卡号 的详情->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-针对已使用的卡号 的详情->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping(value = "cardnumber/information")
	private ResponseMsg information(String cardNumber) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(orderSubscribeService.selectByCard(cardNumber));
		} catch (ProException e) {
			LOG.error("-激活信息出现->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-激活信息异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping("down")
	public void down(@RequestBody OrderRequest request, HttpServletResponse response) {
		try {
			// 设置信息给客户端不解析
			String type = new MimetypesFileTypeMap().getContentType(".xlsx");
			// 设置contenttype，即告诉客户端所发送的数据属于什么类型
			response.setHeader("Content-type", type);
			String filePath = orderSerivce.down(request);
			// 设置编码
			String decode = new String(filePath.substring(filePath.lastIndexOf("/") + 1).getBytes("utf-8"), "iso-8859-1");
			// 设置扩展头，当Content-Type 的类型为要下载的类型时 , 这个信息头会告诉浏览器这个文件的名字和类型。
			response.setHeader("Content-Disposition", "attachment;filename=" + decode);
			FileUtils.download(filePath, response);
			FileUtils.deleteFile(filePath);
		} catch (ProAffairException e) {
			LOG.error("-订单信息-下载出错->{},{}", e.getErrorCode(), e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-订单信息-下载异常->{}", PublicMethod.printErrorTrace(e));
		}
	}
}
